package 双向链表实现;

// 2、无头双向链表实现
public class MyLinkedList {
    static class Node{
        public int val;
        public Node next;
        public Node prev;
        public Node(int val) {
            this.val = val;
        }
    }
    Node head;
    Node last;

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > size()) {
            System.out.println("插入位置错误！");
        }
        if (index == size()) {
            addLast(data);
        }
        if (index <= 1) {
            addFirst(data);
        }
        Node cur = head;
        while (index > 2) {
            index--;
            cur = cur.next;
        }
        Node node = new Node(data);
        node.next = cur.next;
        cur.next.prev = node;
        cur.next = node;
        node.prev = cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        int len = size();
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //当要删除的元素是第一个元素时
                if (cur == head) {
                    //如果只有一个元素情况要考虑
                    if (head.next == null) {
                        head = null;
                        last =null;
                    } else {
                        head = head.next;
                        head.prev = null;
                    }
                    return;
                } else {
                    //删除的是最后一个元素
                    if (cur.next == head) {
                        last = last.prev;
                        cur.prev.next = null;
                    } else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                    return;
                }
            }
            cur = cur.next;
        }
        if(len == size()) {
            System.out.println("没有该元素存在");
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        int len = size();
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //当要删除的元素是第一个元素时
                if (cur == head) {
                    //如果只有一个元素情况要考虑
                    if (head.next == null) {
                        head = null;
                        last =null;
                    } else {
                        head = head.next;
                        head.prev = null;
                    }
                } else {
                    //删除的是最后一个元素
                    if (cur.next == head) {
                        last = last.prev;
                        cur.prev.next = null;
                    } else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
        if(len == size()) {
            System.out.println("没有该元素存在");
        }
    }
    //得到单链表的长度
    public int size(){
        Node cur = head;
        int num = 0;
        while (cur != null) {
            num++;
            cur = cur.next;
        }
        return num;
    }

    public void clear(){
        Node cur = head;
        //将每一个都释放掉（当然也可以直接暴力清除）
        while (cur != null) {
            //这里借用一个指针先记录cur的后一个元素，防止拖链
            Node curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}

